﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.DXErrorProvider;

namespace QuanLyThuNgan
{
    public partial class Frm_DM_LyDoHoan : DevExpress.XtraEditors.XtraForm
    {
        public Frm_DM_LyDoHoan()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DMLyDoHoanCTL lyDoHoanCtl = new DMLyDoHoanCTL();
        DMLyDoHoanDTO lyDoHoanDto = new DMLyDoHoanDTO();
        DataTable dtloadDM = new DataTable();

        //load thong tin du lieu---------------------------------------------
        private void Frm_DM_LyDoHoan_Load(object sender, EventArgs e)
        {
            dtloadDM = lyDoHoanCtl.m_SelectAllDMLyDoHoan();
            gdDMLyDoHoan.DataSource = dtloadDM;

            //set condition load form----------------------------
            gvDMLyDoHoan.OptionsBehavior.ReadOnly = true;
            btnTaoMoi.Enabled = true;
            btnSuu.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
            gvDMLyDoHoan.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //tao moi danh muc----------------------------------------------------
        private void btnTaoMoi_Click(object sender, EventArgs e)
        {
            gvDMLyDoHoan.OptionsBehavior.ReadOnly = false;
            btnLuu.Enabled = false;
            btnTaoMoi.Enabled = false;
            gvDMLyDoHoan.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //reset load du lieu----------------------
            gvDMLyDoHoan.ActiveFilterEnabled = false;
            gvDMLyDoHoan.ClearColumnsFilter();
            gvDMLyDoHoan.OptionsFilter.Reset();
        }
        //sua danh muc---------------------------------------------------------
        private void btnSuu_Click(object sender, EventArgs e)
        {
            gvDMLyDoHoan.OptionsBehavior.ReadOnly = false;
            btnLuu.Enabled = true;
            btnTaoMoi.Enabled = true;
            btnSuu.Enabled = false;
        }
        //xoa du lieu danh muc--------------------------------------------------
        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataRow dr = gvDMLyDoHoan.GetDataRow(gvDMLyDoHoan.FocusedRowHandle);
            if (dr != null)
            {
                lyDoHoanDto.IIDLyDo = Uit.it_Parse.ToInteger(dr["IDLyDo"].ToString());
                lyDoHoanCtl.m_DeleteDMLyDoHoan(lyDoHoanDto);
            }
            XtraMessageBox.Show("Xóa thành công");
            dtloadDM = lyDoHoanCtl.m_SelectAllDMLyDoHoan();
            gdDMLyDoHoan.DataSource = dtloadDM;
            //reset load du lieu----------------------
            gvDMLyDoHoan.ActiveFilterEnabled = false;
            gvDMLyDoHoan.ClearColumnsFilter();
            gvDMLyDoHoan.OptionsFilter.Reset();
        }
        //bo qua thong tin-------------------------------------
        private void btnBoQua_Click(object sender, EventArgs e)
        {
            btnTaoMoi.Enabled = true;
            btnLuu.Enabled = false;
            gvDMLyDoHoan.OptionsBehavior.ReadOnly = true;
            dtloadDM = lyDoHoanCtl.m_SelectAllDMLyDoHoan();
            gdDMLyDoHoan.DataSource = dtloadDM;
            gvDMLyDoHoan.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvDMLyDoHoan.ActiveFilterEnabled = false;
            gvDMLyDoHoan.ClearColumnsFilter();
            gvDMLyDoHoan.OptionsFilter.Reset();
        }

        //luu thong tin danh muc ly do hoan----------------------------------------------------------------
        private void btnLuu_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gdDMLyDoHoan.DataSource;
            if (dt != null)
            {
                for (int i = 0; i < gvDMLyDoHoan.RowCount; i++)
                {
                    DataRow dr = gvDMLyDoHoan.GetDataRow(i);
                    gvDMLyDoHoan.MoveNext();
                    gvDMLyDoHoan.MovePrev();
                    if (dr["TenLyDo"].ToString() != "")
                    {
                        lyDoHoanDto.STenLyDo = dr["TenLyDo"].ToString().Trim();
                        lyDoHoanDto.BStop = Uit.it_Parse.ToBoolean(dr["bStop"].ToString());
                        if (dr["IDLyDo"].ToString() == "")
                        {
                            lyDoHoanCtl.m_InsertDMLyDoHoan(lyDoHoanDto);
                        }
                        else
                        {
                            lyDoHoanDto.IIDLyDo = Uit.it_Parse.ToInteger(dr["IDLyDo"].ToString());
                            lyDoHoanCtl.m_UpdateDMLyDoHoan(lyDoHoanDto);
                        }
                    }

                }
                XtraMessageBox.Show("Lưu thành công");

            }
            dtloadDM = lyDoHoanCtl.m_SelectAllDMLyDoHoan();
            gdDMLyDoHoan.DataSource = dtloadDM;

            btnLuu.Enabled = false;
            btnSuu.Enabled = true;
            btnTaoMoi.Enabled = true;
            gvDMLyDoHoan.OptionsBehavior.ReadOnly = true;
            gvDMLyDoHoan.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

        }

        private void gvDMLyDoHoan_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            GridView view = gvDMLyDoHoan;
            GridColumn colLyDoHoan = view.Columns["TenLyDo"];

            //get value in row---------------------------
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            //{
            string sTenLyDo = view.GetRowCellValue(e.RowHandle, colLyDoHoan).ToString().ToUpper().Trim();
            if (sTenLyDo == "")
            {
                e.Valid = false;
                view.SetColumnError(colLyDoHoan, "Tên lý do không được trống");
            }

            //set duyet gia tri trong vong lap for 0-n
            for (int i = 0; i < gvDMLyDoHoan.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drhc = gvDMLyDoHoan.GetDataRow(i);
                    string test = drhc["TenLyDo"].ToString().ToUpper().Trim();
                    //so sanh gia tri can duyet voi gia tri trong vong lap---
                    if (sTenLyDo == test)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Tên lý do không được trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colLyDoHoan, "Tên lý do không được trùng");
                        return;
                    }
                }
            }

            //}
            btnLuu.Enabled = true;
        }

        private void gvDMLyDoHoan_FocusedColumnChanged(object sender, FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }

        private void gvDMLyDoHoan_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }

        private void gvDMLyDoHoan_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvDMLyDoHoan;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }


    }
}